package 二叉树.cn.mjj;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @author Mxb
 * @version 1.0
 * @date 2020/7/8 11:09
 */
public class _Offer55二叉树的深度 {
    /**
     * 层序遍历计算二叉树的深度
     * 有bug
     */
    class Solution {
        public int maxDepth(TreeNode root) {
            int dept = 0;

            int rowSize = 1;
            Queue<TreeNode> queue = new LinkedList<>();
            queue.offer(root);

            while (!queue.isEmpty()) {
                TreeNode node = queue.poll();
                rowSize--;
                if (node.left != null) {
                    queue.offer(node.left);
                }
                if (node.right != null) {
                    queue.offer(node.right);
                }
                if (rowSize == 0) {
                    rowSize = queue.size();
                    dept++;
                }
            }
            return dept;
        }
    }
}
